import pandas as pd
import re
import os
import json
import openai
from config import API_CONFIG, MODEL_CONFIG

def is_pure_punctuation(text):
    """检查字符串是否只包含标点符号或空白字符"""
    if pd.isna(text) or text.strip() == "":
        return True
    if re.search(r'[\w\u4e00-\u9fff]', str(text), flags=re.UNICODE):
        return False
    cleaned = re.sub(r'[\s.,;:!?，。；：！？、•()\[\]{}"\'«»“”‘’…-]', '', str(text), flags=re.UNICODE)
    return cleaned == ""

def get_client(api_type="DEEPSEEK"):
    """获取API客户端"""
    config = API_CONFIG[api_type]
    return openai.OpenAI(
        api_key=os.getenv(config["API_KEY"], config["API_KEY"]),
        base_url=config["BASE_URL"]
    )

def get_completion(prompt, system_message, model=MODEL_CONFIG["PROOFREADING_MODEL"], temperature=0.3):
    """获取模型补全结果"""
    client = get_client("DEEPSEEK")
    response = client.chat.completions.create(
        model=model,
        temperature=temperature,
        messages=[
            {"role": "system", "content": system_message},
            {"role": "user", "content": prompt},
        ],
    )
    return response.choices[0].message.content

def save_dataframe(df, filename, output_dir="output"):
    """保存DataFrame到Excel文件"""
    os.makedirs(output_dir, exist_ok=True)
    filepath = os.path.join(output_dir, filename)
    df.to_excel(filepath, index=False)
    return filepath